package com.itmk.web.goods_category.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.annotation.Auth;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.goods_category.entity.GoodsCategory;
import com.itmk.web.goods_category.entity.ListParm;
import com.itmk.web.goods_category.entity.SelectType;
import com.itmk.web.goods_category.service.GoodsCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

//分类管理
@Slf4j
@RestController
@RequestMapping("/api/category")
public class GoodsCategoryController {
    @Autowired
    private GoodsCategoryService goodsCategoryService;

    //新增
    @CrossOrigin
    @PostMapping
    public ResultVo add(@RequestBody GoodsCategory goodsCategory){
        log.info("======="+goodsCategory.getCategoryId());
        goodsCategory.setCategoryId(goodsCategoryService.MaxId());
        if (goodsCategoryService.save(goodsCategory)){
            return ResultUtils.success("新增成功");
        }
        else {
            return ResultUtils.error("新增失败");
        }

    }

    //编辑
    @Auth
    @CrossOrigin
    @PutMapping
    public ResultVo edit(@RequestBody GoodsCategory goodsCategory){
        if (goodsCategoryService.updateById(goodsCategory)){
            return ResultUtils.success("编辑成功");
        }
        else {
            return ResultUtils.error("编辑失败");
        }
    }

    //删除
    @Auth
    @CrossOrigin
    @DeleteMapping("/{categoryId}")
    public ResultVo delete(@PathVariable("categoryId") Long categoryId){
        if (goodsCategoryService.removeById(categoryId)){
            return ResultUtils.success("删除成功");
        }
        else {
            return ResultUtils.error("删除失败");
        }
    }
    //分页查询
    @Auth
    @CrossOrigin
    @GetMapping("/list")
    public ResultVo list(ListParm parm){
        //构造分页对象
        Page<GoodsCategory> page = new Page<>(parm.getCurrentPage(),parm.getPageSize());
        LambdaQueryWrapper<GoodsCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(parm.getCategoryName()),
                GoodsCategory::getCategoryName,parm.getCategoryName()).
                orderByDesc(GoodsCategory::getOrderNum);
        Page<GoodsCategory> categoryPage = goodsCategoryService.page(page, queryWrapper);
        return ResultUtils.success("查询成功",categoryPage);
    }

    //小程序分类查询
    @CrossOrigin
    @GetMapping("/getSelectList")
    public ResultVo getSelectList(){
        LambdaQueryWrapper<GoodsCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(GoodsCategory::getOrderNum);
        List<GoodsCategory> list = goodsCategoryService.list(queryWrapper);
        //存储小程序需要的类型
        List<SelectType> selectTypesList = new ArrayList<>();
        //构造小程序需要的类型
        Optional.ofNullable(list).orElse(new ArrayList<>())
                .forEach(item->{
                    SelectType type = new SelectType();
                    type.setLabel(item.getCategoryName());
                    type.setValue(item.getCategoryId());
                    selectTypesList.add(type);
                });
        return ResultUtils.success("查询成功",selectTypesList);
    }

    //小程序分类接口
    @CrossOrigin
    @GetMapping("/getCateList")
    public ResultVo getCateList(){
        List<GoodsCategory> list = goodsCategoryService.list();
        return ResultUtils.success("查询成功",list);
    }
}
